<?php

namespace app\wechat\service;

use app\admin\service\BaseService;
use app\wechat\model\ApplicationModel;
use Exception;

class ApplicationService extends BaseService
{

     /**
     * 获取应用列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function getApplicationList($params = [], $page = 1, $limit = 15){
        $where = [];
        //应用类型搜索
        if(isset($params['account_type']) && $params['account_type']){
            $where['account_type'] = $params['account_type'];
        }
        //应用列表
        $list = ApplicationModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as &$value) {

        }
        //应用总数
        $count = ApplicationModel::where($where)->count();
        return self::createList($list,$count);
    }

    /**
     * 添加或者编辑应用
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param [type] $params
     * @return void
     */
    public static function addOrEditData($params){
        try {
            if(empty($params['application_name'])){
                return self::createFail('应用名称不能为空');
            }
            if(empty($params['account_type'])){
                return self::createFail('应用类型不能为空');
            }
            if(empty($params['app_id'])){
                return self::createFail('应用app_id不能为空');
            }
            if(empty($params['secret'])){
                return self::createFail('应用secret不能为空');
            }
            $data = [
                'application_name'  => $params['application_name'],
                'account_type'      => $params['account_type'],
                'app_id'            => $params['app_id'],
                'secret'            => $params['secret'],
                'mch_id'            => $params['mch_id'],
                'secret'            => $params['secret'],
                'mch_key'           => $params['mch_key'],
                'secret'            => $params['secret'],
                'cert_path'         => $params['cert_path'],
                'key_path'          => $params['key_path'],
                'token'             => $params['token'],
                'aes_key'           => $params['aes_key'],
            ];
            if(isset($params['id']) && $params['id']){
                $res = ApplicationModel::where('id',$params['id'])->save($data);
            }else{
                $res = ApplicationModel::create($data);
            }
            if($res !== false){
                return self::createSuccess('保存成功');
            }
            return self::createFail('保存失败');
        } catch (Exception $e) {
            //输出错误信息
            return self::createFail($e->getMessage());
        }
    }

    /**
     * 应用删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param [type] $adminId
     * @return void
     */
    public static function applicationDelete($id){
        if(!$id){
            return self::createFail('参数错误!');
        }
        $applicationDetail = ApplicationModel::where('id',$id)->findOrEmpty();
        if(!$applicationDetail){
            return self::createFail('应用不存在!'); 
        }
        $result = $applicationDetail->delete();
        if($result !== false){
            return self::createSuccess('删除成功');
        }
        return self::createSuccess('删除失败');
    }
}